package cn.com.qingz.modules.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.model.DefaultResp;
import cn.com.qingz.modules.user.model.Resource;
import cn.com.qingz.modules.user.model.Role;
import cn.com.qingz.modules.user.service.RoleService;
import cn.com.qingz.modules.user.vo.RoleAuthorityVo;
import cn.com.qingz.modules.user.vo.RoleCondition;

@Controller
@RequestMapping("role/*")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色管理页面
     * @return
     */
    @RequestMapping("roleManagePage")
    public String roleManagePage() {
        return "role/roleManage";
    }

    /**
     * 角色授权页面
     * @return
     */
    @RequestMapping("setAuthorityPage")
    public String setAuthorityPage() {
        return "role/setAuthority";
    }

    /**
     * 查询系统里面所有角色
     * @return
     */
    @RequestMapping("queryAllRoles")
    @ResponseBody
    public DefaultResp<List<Role>> queryAllRoles() {
        DefaultResp<List<Role>> res = new DefaultResp<List<Role>>();
        List<Role> data = roleService.getAllRoles();
        res.setData(data);
        return res;
    }

    //分页查询角色
    @RequestMapping("queryRolePage")
    @ResponseBody
    public Map<String, Object> queryRolePage(RoleCondition condition, @RequestParam(required = false, defaultValue = "0") int offset,
            @RequestParam(required = false, defaultValue = "10") int limit) {
        final int pageNo = offset / limit + 1;
        PageList<RoleAuthorityVo> list = roleService.queryRolePage(condition, pageNo, limit);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("total", list.getPaginator().getTotalCount());
        res.put("rows", list);

        return res;
    }

    /**
     * 获取某个用户的角色
     * @param userid
     * @return
     */
    @RequestMapping("getUserRole")
    @ResponseBody
    public DefaultResp<List<Role>> getUserRole(Long userid) {
        DefaultResp<List<Role>> res = new DefaultResp<List<Role>>();
        List<Role> data = roleService.getUserRole(userid);
        res.setData(data);
        return res;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("addRole")
    @ResponseBody
    public DefaultResp<String> addRole(Role role) {
        DefaultResp<String> res = new DefaultResp<String>();
        roleService.doAddRole(role);
        return res;
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("updateRole")
    @ResponseBody
    public DefaultResp<String> updateRole(Role role) {
        DefaultResp<String> res = new DefaultResp<String>();
        roleService.doUpdateRole(role);
        return res;
    }

    /**
     * 禁用角色
     * @param roleids
     * @return
     */
    @RequestMapping("disableRoles")
    @ResponseBody
    public DefaultResp<String> disableRoles(String roleids) {
        DefaultResp<String> res = new DefaultResp<String>();
        roleService.doDisableRoles(roleids);
        return res;
    }

    @RequestMapping("addOrUpdateRole")
    @ResponseBody
    public DefaultResp<String> addOrUpdateRole(@Valid Role role, BindingResult result) {
        DefaultResp<String> res = new DefaultResp<String>();

        //校验
        if (result.hasErrors()) {
            List<FieldError> list = result.getFieldErrors();
            throw new DicmfException(SystemConstantes.ERROR, list.get(0).getDefaultMessage());
        }

        if (role.getId() == null) {
            roleService.doAddRole(role);
        } else {
            roleService.doUpdateRole(role);
        }

        return res;
    }

    /**
     * 启用 / 禁用
     * @param userid
     * @param role_ids
     * @return
     */
    @RequestMapping("onOff")
    @ResponseBody
    public DefaultResp<String> onOff(Integer roleid, String status) {
        DefaultResp<String> res = new DefaultResp<String>();
        if ("Y".equals(status) || "N".equals(status)) {
            roleService.doOnOff(roleid, status);
        } else {
            throw new DicmfException(SystemConstantes.ERROR, "status 参数值错误 Y/N");
        }
        return res;
    }

    /**
     * 检查角色代码是否重复
     * @param code
     * @return
     */
    @RequestMapping("checkRoleCode")
    @ResponseBody
    public String checkRoleCode(String code) {
        boolean isRepeat = roleService.checkRoleCode(code);
        if (!isRepeat) {
            return "success";
        }
        return "fail";
    }

    /**
     * 查询所有权限资源
     * @param role
     * @return
     */
    @RequestMapping("getAllResource")
    @ResponseBody
    public DefaultResp<List<Resource>> getAllResource(Role role) {
        DefaultResp<List<Resource>> res = new DefaultResp<List<Resource>>();
        List<Resource> data = roleService.getAllResource();
        res.setData(data);
        return res;
    }

}
